package com.le.tester.leetcode;

import com.le.tester.leetcode.common.TreeNode;

/**
 * createTime：2021/8/25 15:18
 * description：ArrayToBST
 */
public class ArrayToBST {
    public TreeNode sortedArrayToBST(int[] nums) {
        return help(nums, 0, nums.length - 1);
    }

    public TreeNode help(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = (left + right) / 2;
        TreeNode root = new TreeNode(nums[mid]);
        //递归改变树的节点位置
        root.left = help(nums, left, mid - 1);
        root.right = help(nums, mid + 1, right);
        return root;
    }

    public static void main(String[] args) {
        new ArrayToBST().sortedArrayToBST(new int[]{-10, -3, 0, 5, 9});
    }
}
